<template>
  <div class="chart-container">
    <div class="one-top-right-banner">系统被调用失败次数</div>
    <div class="chart" ref="oneTopRight"></div>
  </div>
</template>

<script>
import * as echarts from 'echarts'
import { mapState } from 'vuex'
import 'echarts-gl'
import { getPie3D } from './chart' // 工具类js，页面路径自己修改
import { fontSize } from '../../utils/tools'
const color = ['#3abcd0', '#f5c56b', '#1197ef', '#6cd78b', '#a65ef5']

export default {
  name: 'oneTopRight',
  data () {
    return {
      changeState: 0,
      optionDataOwn: '',
      optionData: [],
      statusChart: '',
      option: {}
    }
  },
  watch: {
    initData: {
      deep: true,
      handler (val) {
        console.log('数据改变了')
        this.optionDataOwn = JSON.parse(
          JSON.stringify(this.initData.systemCalledFailNum)
        )
        this.optionData = this.optionDataOwn
        this.setLabel()
        this.changeState++
      }
    },
    changeState: {
      immediate: true,
      handler (val) {
        if (
          this.statusChart !== null &&
          this.statusChart !== '' &&
          this.statusChart !== undefined
        ) {
          this.statusChart.dispose() // 销毁
        }
        this.statusChart = echarts.init(this.$refs.oneTopRight)
        this.initChart(this.statusChart)
        this.$forceUpdate()
      }
    }
  },
  computed: {
    ...mapState(['initData'])
  },
  created () {
    this.optionDataOwn = JSON.parse(
      JSON.stringify(this.initData.systemCalledFailNum)
    )
    this.optionData = this.optionDataOwn
    this.setLabel()
  },
  mounted () {
    // console.log('123')
    this.statusChart = echarts.init(this.$refs.oneTopRight)
    this.initChart(this.statusChart)
    window.addEventListener('resize', () => {
      this.statusChart.resize()
    })
    this.$forceUpdate()
  },
  methods: {
    // 初始化label样式
    setLabel () {
      this.optionData.forEach((item, index) => {
        item.itemStyle = {
          color: color[index]
        }
        item.label = {
          show: true,
          color: color[index],
          formatter: ['{d|{d}%}', '{c|{c}条}'].join('\n'), // 用\n来换行
          rich: {
            c: {
              fontSize: fontSize(20),
              lineHeight: fontSize(25),
              color: color[index]
            },
            d: {
              fontSize: fontSize(20),
              lineHeight: fontSize(25),
              color: color[index],
              align: 'left'
            }
          }
        }
        item.labelLine = {
          length: fontSize(45),
          lineStyle: {
            type: 'dashed',
            cap: 'round',
            width: fontSize(2),
            color: color[index]
          }
        }
      })
    },
    // 图表初始化
    initChart (name) {
      let myChart = name
      // 传入数据生成 option, 构建3d饼状图, 参数工具文件已经备注的很详细
      this.option = getPie3D(this.optionData, 0.8, 240, 48, 20, 1)
      // myChart.setOption(this.option)
      // 是否需要label指引线，如果要就添加一个透明的2d饼状图并调整角度使得labelLine和3d的饼状图对齐，并再次setOption
      this.option.series.push({
        name: 'pie2d', // 自己根据场景修改
        backgroundColor: 'transparent',
        type: 'pie',
        label: {
          opacity: 1,
          fontSize: fontSize(6),
          lineHeight: fontSize(10)
        },
        startAngle: -40, // 起始角度，支持范围[0, 360]。
        clockwise: false, // 饼图的扇区是否是顺时针排布。上述这两项配置主要是为了对齐3d的样式
        radius: ['20%', '50%'],
        center: ['50%', '50%'],
        data: this.optionData,
        itemStyle: {
          opacity: 0 // 这里必须是0，不然2d的图会覆盖在表面
        }
      })
      myChart.setOption(this.option)
      // this.bindListen(this.statusChart)
    }
  }
}
</script>

<style lang="less" scoped>
.chart-container {
  position: relative;
  width: 100%;
  height: 100%;
  background: url('../../assets/image/thin.png') no-repeat 0px 0px;
  background-size: 100% 100%;
  // background: #061a32;
  .one-top-right-banner {
    width: 100%;
    height: 110px;
    padding-left: 13%;
    font-size: 35px;
    font-family: PingFangSC-Medium, PingFang SC;
    font-weight: 500;
    color: #66e2e2;
    display: flex;
    justify-content: flex-start;
    align-items: center;
    background: url('../../assets/image/thinTipbg.png') no-repeat 0px 0px;
    background-size: 100% 100%;
  }
  .chart {
    width: 750px;
    height: 770px;
  }
}
</style>
